package com.dq.Controller;

import com.dq.Pojo.Percentage;
import com.dq.Pojo.Result;
import com.dq.Service.PercentageService;
import org.apache.poi.ss.formula.functions.Intercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/percentage")
public class PercentageController {
    @Autowired
    private PercentageService percentageService;
    //修改和添加
    @PostMapping("/addORupdate")
    public Result addORupdate(@RequestBody Percentage percentage){
        float total = percentage.getAttend()+ percentage.getClasstask()+ percentage.getAftertask()+ percentage.getExperiment();
        float total1= percentage.getUsualscore()+ percentage.getFinalscore();
        if (total ==1&&total1==1){
            if (percentage.getPercentId()==null){
                percentageService.add(percentage);
                return Result.success();
            }else {
                percentageService.update(percentage);
                return Result.success();
            }
        }else {
            return Result.error("填写分数比例不成立，请重新填写!!!");
        }
    }
    //根据课程ID和班级ID查找是否已存在分数占比
    @GetMapping("/getByid")
    public Result getByid(@RequestParam Integer courseId,
                          @RequestParam Integer classId){
        Percentage percentage=percentageService.getByid(classId,courseId);
        if(percentage==null){
            return Result.error("未填写分数占比!!!");
        }else {
            return Result.success(percentage);
        }

    }
}
